public class HuffmanTree implements Comparable<HuffmanTree> {

        private static int timer = 0;
        
        public final int time;
        public final int frequency;
        public final char character;
        
        public final HuffmanTree left;
        public final HuffmanTree right;
        
        public HuffmanTree(char character, int frequency) {
                this.time = timer++;
                this.character = character;
                this.frequency = frequency;
                this.left = null;
                this.right = null;
        }
        
        public HuffmanTree(HuffmanTree less, HuffmanTree more) {
                this.time = timer++;
                this.character = 0;
                this.frequency = less.frequency + more.frequency;
                this.left = less;
                this.right = more;
        }
        
        @Override
        public int compareTo(HuffmanTree that) {
                if (this.frequency == that.frequency) {
                        if (this.character == that.character) {
                                return this.time - that.time;
                        } else {
                                return this.character - that.character;
                        }
                } else {
                        return this.frequency - that.frequency;
                }
        }
        
        @Override
        public boolean equals(Object obj) {
                if (obj instanceof HuffmanTree) {
                        return ((HuffmanTree) obj).time == time;
                }
                return false;
        }
        
        @Override
        public int hashCode() {
                return time;
        }
}